﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class Pinballs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!Page.IsPostBack)
        {
            sel_tour.DataSource = Tournament.getAvailableTournaments();
            sel_tour.DataTextField = "Tournament_Name";
            sel_tour.DataValueField = "Pk_Tournament";
            sel_tour.DataBind();
        }
        AjaxPro.Utility.RegisterTypeForAjax(typeof(Pinballs));
    }
    [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.Read)]
    public int MakePin(string p_Name,string p_Acron,int p_Number,int p_Tournament, bool p_AddQualify, bool p_AddPlayoffs, int Id_IPDB)
    {
        try
        {
            int res = -1;
            Machine oMachine = new Machine(p_Name, p_Acron, Convert.ToByte(p_Number));
            if (Id_IPDB >= 0)
            {
                oMachine.Id_IPDB = Id_IPDB;
            }
            res= Machine.InsertMachine(oMachine, p_Tournament);
            
            if (p_AddQualify == true) { 
            //Añadimos la máquina a los jugadores de la clasificación si ya la hemos generado.

            Tournament CurrentTournament = new Tournament(p_Tournament);
            List<Player> CurrentPlayers = Player.GetPlayersByTournament(CurrentTournament.Pk_Tournament);
            if (CurrentPlayers != null)
            {
                ////Free play based add to all players the new added machine
                foreach (Player iPlayer in CurrentPlayers)
                {
                    QualifyRound oRound = new QualifyRound(CurrentTournament.Pk_Tournament,
                                                                      iPlayer.Pk_Player,
                                                                      iPlayer.Player_Nick,
                                                                      -1,
                                                                      iPlayer.Fk_PlayerZone,
                                                                      res,
                                                                      oMachine.Machine_Acron,
                                                                      oMachine.Machine_Number,
                                                                      -1,
                                                                      -1,
                                                                      iPlayer.Player_Number
                                                                      );
                    QualifyRounds.AddQualify(oRound);
                }
            }
          
            }

            if (p_AddPlayoffs == true)
            {
                //Añadimos la máquina a los jugadores de la clasificación si ya la hemos generado.

                Tournament CurrentTournament = new Tournament(p_Tournament);
                PlayoffRounds oPlayoffRounds = new PlayoffRounds(p_Tournament);
                List<Player> CurrentPlayers = oPlayoffRounds.GetPlayersForTournament(p_Tournament);
                ////Free play based add to all players the new added machine
                if (CurrentPlayers != null)
                {
                    foreach (Player iPlayer in CurrentPlayers)
                    {
                        PlayoffRound oRound = new PlayoffRound(CurrentTournament.Pk_Tournament,
                                                                          iPlayer.Pk_Player,
                                                                          iPlayer.Player_Nick,
                                                                          -1,
                                                                          iPlayer.Fk_PlayerZone,
                                                                          res,
                                                                          oMachine.Machine_Acron,
                                                                          oMachine.Machine_Number,
                                                                          -1,
                                                                          -1,
                                                                          iPlayer.Player_Number
                                                                          );
                        PlayoffRounds.AddPlayoff(oRound);
                    }

                }
            }
            return res;

        }
        catch
        {
            return -1;
        }
    }
 
   
}
